/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "EPUB_Document.h"

EPUB_Shared_Document EPUB_Document::s_instance = EPUB_Shared_Document();

EPUB_Document::EPUB_Document():
		m_pAuthor(0),
		m_pTitle(0),
		m_pRoot(new EPUB_Element(EDE_Html))
{
	// Now we need add to document some basic dom structure - html, head and body



	EPUB_Shared_Element pHead(new EPUB_Element(EDE_Head));
	EPUB_Shared_Element pBody(new EPUB_Element(EDE_Body));

	m_pRoot->addChild(pHead);
	m_pRoot->addChild(pBody);

	m_pRoot->setAttribute("xmlns", new UT_UTF8String("http://www.w3.org/1999/xhtml"));
}

EPUB_Shared_Document EPUB_Document::getInstance()
{
	if (s_instance == EPUB_Shared_Document())
		s_instance = EPUB_Shared_Document(new EPUB_Document());

	return s_instance;
}

EPUB_Shared_Document EPUB_Document::getNewInstance()
{
	s_instance = EPUB_Shared_Document(new EPUB_Document());
	return s_instance;
}


EPUB_Shared_Element EPUB_Document::getDocumentRoot()
{
	return m_pRoot;
}

EPUB_Shared_Element EPUB_Document::getDocumentBody()
{
	for(int i = 0; i < m_pRoot->getChildren().size(); i++)
	{
		if (m_pRoot->getChildren().at(i)->getType() == EDE_Body)
		{
			return m_pRoot->getChildren().at(i);
		}
	}

	//TODO: Add assertion here - this must not happen
}

void EPUB_Document::loadMetadata(PD_Document *pDoc)
{
	pDoc->getMetaDataProp(PD_META_KEY_TITLE, *m_pTitle);
	pDoc->getMetaDataProp(PD_META_KEY_CREATOR, *m_pAuthor);
}

void EPUB_Document::addFile(EPUB_Shared_EmbeddedFile file)
{
	m_files.push_back(file);
}
UT_UTF8String* EPUB_Document::getAuthor()
{
	return m_pAuthor;
}

UT_UTF8String* EPUB_Document::getTitle()
{
	return m_pTitle;
}

std::list<EPUB_Shared_EmbeddedFile>& EPUB_Document::getFiles()
{
	return m_files;
}

